package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.service.ManageService;
import io.minio.*;
import io.minio.errors.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/5/9 14:31
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/product")
public class FileUpLoadController {

    @Autowired
    private ManageService manageService;
    /*
    minio:
      endpointUrl: http://192.168.200.130:9000
      accessKey: admin
      secreKey: admin123456
      bucketName: atguigus
     */

    //  springMVC 的时候讲过一个文件上传类：
    @PostMapping("/fileUpload")
    public Result fileUpload(MultipartFile file){
        //  声明一个url 变量
        String url="";
        //  调用服务层方法
        url = manageService.fileUpLoad(file);
        //  返回数据。
        return Result.ok(url);
    }

    //  ctrl+alt+m
    //    private String fileUpLoad(MultipartFile file) {
    //
    //    }

    public static void main(String[] args) {
        //  字符串截取：subString();
        String fileName = "atgui.gu.png";
        //  包前，不包后
        System.out.println(fileName.lastIndexOf("."));
        System.out.println(fileName.substring(fileName.lastIndexOf(".")));
        System.out.println(UUID.randomUUID().toString().replaceAll("-","")+fileName.substring(fileName.lastIndexOf(".")));
        System.out.println(FilenameUtils.getExtension(fileName));
    }
}
